package com.bmob.lover.ui.fragment

import android.view.View
import androidx.fragment.app.viewModels
import com.qmuiteam.qmui.widget.grouplist.QMUICommonListItemView
import com.theone.common.ext.*
import com.bmob.lover.R
import com.bmob.lover.data.model.bean.LoverBind
import com.bmob.lover.data.model.bean.User
import com.bmob.lover.databinding.FragmentWeBinding
import com.bmob.lover.ui.fragment.admin.AdminFragment
import com.bmob.lover.ui.fragment.anniversary.AnniversaryFragment
import com.bmob.lover.ui.fragment.base.BaseFragment
import com.bmob.lover.ui.fragment.cost.CostFragment
import com.bmob.lover.ui.fragment.festival.FestivalBgFragment
import com.bmob.lover.ui.fragment.lol.LOLFragment
import com.bmob.lover.ui.fragment.resentment.ResentmentFragment
import com.bmob.lover.ui.fragment.user.UserInfoFragment
import com.bmob.lover.viewmodel.WeViewModel
import com.bmob.lover.viewmodel.request.LoverBindRequestViewModel
import com.theone.mvvm.ext.qmui.addToGroup
import com.theone.mvvm.ext.qmui.createItem
import java.util.*

//  ┏┓　　　┏┓
//┏┛┻━━━┛┻┓
//┃　　　　　　　┃
//┃　　　━　　　┃
//┃　┳┛　┗┳　┃
//┃　　　　　　　┃
//┃　　　┻　　　┃
//┃　　　　　　　┃
//┗━┓　　　┏━┛
//    ┃　　　┃                  神兽保佑
//    ┃　　　┃                  永无BUG！
//    ┃　　　┗━━━┓
//    ┃　　　　　　　┣┓
//    ┃　　　　　　　┏┛
//    ┗┓┓┏━┳┓┏┛
//      ┃┫┫　┃┫┫
//      ┗┻┛　┗┻┛
/**
 * @author The one
 * @date 2021-04-23 13:12
 * @describe TODO
 * @email 625805189@qq.com
 * @remark
 */
class WeFragment : BaseFragment<WeViewModel, FragmentWeBinding>(), View.OnClickListener {

    private lateinit var mMenstrual: QMUICommonListItemView
    private lateinit var mSetting: QMUICommonListItemView
    private lateinit var mLOL: QMUICommonListItemView
    private lateinit var mFestivalBg: QMUICommonListItemView
    private lateinit var mResentment: QMUICommonListItemView
    private lateinit var mAccountBook: QMUICommonListItemView
    private lateinit var mAnniversary: QMUICommonListItemView

    private var isBindLover = false

    override fun showTopBar(): Boolean = true

    override fun isNeedChangeStatusBarMode(): Boolean = false

    private val mLoverBindRequest: LoverBindRequestViewModel by viewModels()

    override fun translucentFull(): Boolean = true

    override fun initView(root: View) {
        getTopBar()?.run {
            setBackgroundAlpha(0)
            updateBottomDivider(0, 0, 0, 0)
        }
        getDataBinding().swipeRefresh.setOnRefreshListener {
            mLoverBindRequest.requestServer()
        }
        getDataBinding().mGroupListView.run {
            mMenstrual = createItem(R.string.menstrual, drawable = R.drawable.ic_menstrual)
            mSetting = createItem(R.string.setting, drawable = R.drawable.ic_we_setting)
            mLOL = createItem(R.string.lol, drawable = R.drawable.svg_lol)
            mFestivalBg = createItem(R.string.festival_bg, drawable = R.drawable.svg_festival)
            mResentment = createItem(R.string.resentment, drawable = R.drawable.svg_festival)
            mAccountBook = createItem(R.string.account_book, drawable = R.drawable.svg_account_book)
            mAnniversary = createItem(R.string.anniversary, drawable = R.drawable.svg_anniversary)
            addToGroup(
                mMenstrual,
                mResentment,
                mFestivalBg,
                mAccountBook,
                mAnniversary,
                listener = this@WeFragment
            )
            addToGroup(mLOL, listener = this@WeFragment)
            addToGroup(mSetting, listener = this@WeFragment)
        }

    }

    override fun initData() {
        isBindLover = null != mAppVm.getLoverBindLiveData().value
        getViewModel().isBindLover.set(isBindLover)
        mAppVm.getUserInfoLiveData().value?.setUserInfo()
        mAppVm.getLoverBindLiveData().value?.setLoverInfo()
        getDataBinding().swipeRefresh.isEnabled = isBindLover
    }

    private fun User.setUserInfo() {
        with(getViewModel()) {
            userNick.set(nickname)
            userIcon.set(avatar)
            isAdmin.set(this@setUserInfo.isAdmin)
        }
        mResentment.visible(isBindLover && isGirl())
        mMenstrual.visible(isGirl() || (isBoy() && isBindLover))
        mFestivalBg.visible(isAdmin)
    }

    private fun LoverBind.setLoverInfo() {
        loverDate.getDate()?.let { date ->
            val end = if (null == breakDate) Date() else breakDate.getDate()!!
            val dis = getTimeDistance(date, end)
            getViewModel().loverDate.set(dis.toString())
        }
        mAppVm.getLoverInfo()?.let {
            with(getViewModel()) {
                loverNick.set(it.nickname)
                loverIcon.set(it.avatar)
            }
        }
    }

    override fun createObserver() {
        mAppVm.getUserInfoLiveData().observe(this) {
            it.setUserInfo()
        }
        mAppVm.getLoverBindLiveData().observe(this){
            it.setLoverInfo()
        }
        mLoverBindRequest.getResponseLiveData().observe(this) {
            if (!it.isNullOrEmpty()) {
                mAppVm.setLoverBind(it[0])
            }
        }
        mLoverBindRequest.getFinallyLiveData().observe(this) {
            getDataBinding().swipeRefresh.isRefreshing = false
        }
    }

    override fun onClick(v: View?) {
        startFragment(
            when (v) {
                mMenstrual -> MenstrualFragment()
                mLOL -> LOLFragment()
                mFestivalBg -> FestivalBgFragment()
                mSetting -> SettingFragment()
                mResentment -> ResentmentFragment()
                mAccountBook -> CostFragment()
                mAnniversary -> AnniversaryFragment()
                else -> SettingFragment()
            }
        )
    }

    override fun getBindingClick() = ProxyClick()

    inner class ProxyClick {

        private var mClickTimes: Int = 0
        private var mClickTime: Long = 0

        fun admin() {
            val current = System.currentTimeMillis()
            if (current - mClickTime < 500) {
                mClickTimes++
                if (mClickTimes == 10) {
                    startFragment(AdminFragment())
                    mClickTimes = 0
                }
            } else {
                mClickTimes = 0
            }
            mClickTime = current
        }

        fun userInfo() {
            mAppVm.getUserInfoLiveData().value?.let {
                startFragment(UserInfoFragment.newInstance(it))
            }
        }

        fun loverInfo() {
            mAppVm.getLoverInfo()?.let {
                startFragment(UserInfoFragment.newInstance(it))
            }
        }

    }

}